<?php
/**
 * 
 * @author chenqi
 * @uses 申请小鸽
 * @date 2015-07-10
 *
 */
class Apply extends WX_Controller{
   public function __construct()
	{
		parent::__construct();
		$this->load->model('area_model');
		$this->load->model('project_model');
		
	}
	
	//填写用户名手机号完成申请  申请小鸽
	public function reg()
	{
		//判断是否成为小鸽
		$userid = $this->user['id'];
		$this->load->model('master_model');
		$master=$this->master_model->checkMaster(array('user_id'=>$userid,'check_flag !='=>3));
		if($master){
			if($master['check_flag'] == 1){
				echo "<script>alert('你已是小鸽,请勿重新申请！');window.location.replace('/wap/home?open_id=1');</script>";exit;
			}else if($master['check_flag'] == 2){
				echo "<script>alert('你的小鸽申请还未审核成功！');window.location.replace('/wap/home?open_id=1');</script>";exit;
			}
		}
		
		
		
		$province = $this->city();//获取省信息
		
		$service_type =$this->project_model->getProjectlist(array("show_flag"=>1)); //获取服务种类

		$data['province'] = $province;
		$data['service_type'] = $service_type;
		$this->load->view('/wap/reg.html',$data);
	}
	
	//获取城市联动
	private function city($parentno=0,$arealevel=1){

		$where["parentno"] = $parentno;
		$where["arealevel"] = $arealevel;
		
		$count=$this->area_model->getCount($where);

		if($count)
			$province = $this->area_model->getArea($where,$count,0,"sort ASC");
		
		return $province;
	}
	//城市联动接口
	public function area(){
		$parentno = $this->input->get('parentno');
		$arealevel= $this->input->get('arealevel');
		$prov=$this->city($parentno,$arealevel);
		echo json_encode($prov);
	}
	
	//提交申请
	public function submit(){
		$userid = $this->user['id'];
		//判断是否已申请		
		$name = $this->input->get('name');
		$phone = $this->input->get('phone');
		$identy = $this->input->get('identy');
		$area = $this->input->get('area');
		$area_no = $this->input->get("area_no");//地区编码
		$service = $this->input->get('service');
		$service_value = $this->input->get('service_value');
		$service_id = $this->input->get('service_id');
		$ss = array();
		foreach ($service_id as $k=>$v){
			$ss[$v] = $service_value[$k];
		}
		$ss = serialize($ss);
		
		$areas["detail"] = $area;
		$areas["area_no"] = $area_no;
		
		$data = array(
				"user_id"=>$userid,
				"server_card"=>$identy,
				"name"=>$name,
				"phone"=>$phone,
				"classes"=>$ss,
				"areas"=>serialize($areas),
				"server_flag"=>1,
				"check_flag"=>2,
				"update_time"=>$this->current_time,
				"create_time"=>$this->current_time
				);
		
		$this->load->model('master_model');
		
		$res = $this->master_model->addMaster($data);
		if($res){
			$this->response(array('status'=>true, 'data'=>'申请成功！'), 200);
		}
		$this->response(array('status'=>false, 'data'=>'预约出错，请重试！'));
		
	}
 
 	

}